/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.DirectSerialConfigModel;
import org.testis.gui.mvc.view.testbed.DirectSerialConfigView;
import org.testis.orm.testbed.datahandlers.DirectSerialConfigDataHandler;
import org.testis.orm.testbed.refined.DirectSerialConfigFactory;
import org.testis.orm.testbed.refined.DirectSerialConfigRefined;
import org.testis.utils.tree.TbedMutableTreeNode;

/**
 *
 * @author selcuk
 */
public class DirectSerialConfigController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String ID_PROPERTY = "Id";
    public static final String NAME_PROPERTY = "Name";
    public static final String PORT_PROPERTY = "Port";
    public static final String BOUNDARY_PROPERTY = "Boundary";
    public static final String DATABIT_PROPERTY = "DataBit";
    public static final String PARITY_PROPERTY = "Parity";
    public static final String STOPBIT_PROPERTY = "StopBit";
    public static final String FLOWCONTROL_PROPERTY = "FlowControl";
    private TbedMutableTreeNode m_selectedNode = null;

    public DirectSerialConfigController() {
        super();
    }

    private void initialize(boolean editMode) {
        DirectSerialConfigModel model = new DirectSerialConfigModel();
        DirectSerialConfigView view = new DirectSerialConfigView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationEditConfig(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof DirectSerialConfigRefined) {
            m_selectedNode = node;
            DirectSerialConfigRefined refined = getSelectedConfig();
            if (refined == null) {
                System.out.println(
                        "Not found design part for DirectSerialConfigRefined");
                return;
            }
            initialize(true);
            setModelProperty(ID_PROPERTY, refined.getId());
            setModelProperty(NAME_PROPERTY, refined.getName());
            setModelProperty(BOUNDARY_PROPERTY, refined.getBoundary());
            setModelProperty(DATABIT_PROPERTY, refined.getDataBit());
            setModelProperty(FLOWCONTROL_PROPERTY, refined.getFlowControl());
            setModelProperty(PARITY_PROPERTY, refined.getParity());
            setModelProperty(PORT_PROPERTY, refined.getPort());
            setModelProperty(STOPBIT_PROPERTY, refined.getStopBit());
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationNewConfig() {
        initialize(false);
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    private DirectSerialConfigRefined getSelectedConfig() {
        DirectSerialConfigRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof DirectSerialConfigRefined) {
            result = (DirectSerialConfigRefined) obj;
        }
        return result;
    }

    public void operationSaveConfig(int id, String name,
            String boundary, int dataBit, int flowControl,
            int parity, int port, int stopBit) {
        boolean result = DirectSerialConfigDataHandler.newInstance().saveConfig(
                id, name, boundary, dataBit, flowControl, parity, port, stopBit);
        if (!result) {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to edit DirectSerialConfig record");
        }
    }

    public void operationAddConfig(String name,
            String boundary, int dataBit, int flowControl,
            int parity, int port, int stopBit) {
        DirectSerialConfigRefined refined = DirectSerialConfigFactory.newInstance().create(
                name, boundary, dataBit, flowControl, parity, port, stopBit);
        if (refined != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    refined);
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to add DirectSerialConfig record");
        }
    }
}
